package com.coocaa.ops.admin.v1.upload.util;

import com.coocaa.ops.admin.v1.upload.properties.UploadProperties;
import com.coocaa.ops.admin.v1.upload.enums.UploadCategoryEnums;
import com.coocaa.ops.admin.v1.upload.exception.HostNotFoundException;

/**
 * @author bijiahao
 * @date : 2019/2/18.
 * @description: 上传文件 路径工具类
 *
 */
public final class UploadFilePathUtil {

    private static final String UN_ZIP_DIR = "unzip";
    public static final String DIR_MACOSX = "__MACOSX";

    /**
     * 获取最终的url
     *
     * @param uploadCategoryEnums
     * @param remoteFilePath
     * @return
     */
    public static String getFinalUrl(UploadCategoryEnums uploadCategoryEnums, String remoteFilePath) {
        StringBuilder url = new StringBuilder();
        switch (uploadCategoryEnums) {
            case IMG:
                url.append(UploadProperties.Constants.IMG_HOST);
                break;
            case APK:
                url.append(UploadProperties.Constants.APK_HOST);
                break;
            case ART:
                url.append(UploadProperties.Constants.ART_HOST);
                break;
            default:
                throw new HostNotFoundException("没有配置对应的FTP服务器地址");
        }
        //
        int indexPos = uploadCategoryEnums.name().length() + 1;
        String relativePath = remoteFilePath.substring(indexPos);
        //前一步保留了【"/"】,所以这里不需要加
        url.append(relativePath);
        return url.toString();
    }

    /**
     * 获取远程上传路径-apk
     *
     * @param newFileName
     * @return
     */
    public static String getRemoteFilePathForApk(String newFileName) {
        StringBuilder path = new StringBuilder();
        path.append("/")
                .append(UploadCategoryEnums.APK.name().toLowerCase())
                .append("/")
                .append(UploadProperties.Constants.ENV_DIR)
                .append("/")
                .append(UploadCategoryEnums.APK.name().toLowerCase())
                .append("/")
                .append(newFileName);
        return path.toString();

    }

    /**
     * 获取远程上传路径-img
     *
     * @param newFileName
     * @return
     */
    public static String getRemoteFilePathForImg(String newFileName) {
        StringBuilder path = new StringBuilder();
        path.append("/")
                .append(UploadCategoryEnums.IMG.name().toLowerCase())
                .append("/")
                .append(UploadProperties.Constants.ENV_DIR)
                .append("/")
                .append(newFileName);
        return path.toString();
    }

    /**
     * 获取远程上传路径-img
     *
     * @param type
     * @param rootDir
     * @param fileName
     * @return
     */
    public static String getRemoteFilePathForImg(String type, String rootDir, String fileName) {
        StringBuilder path = new StringBuilder();
        path.append("/")
                .append(UploadCategoryEnums.IMG.name().toLowerCase())
                .append("/")
                .append(UploadProperties.Constants.ENV_DIR)
                .append("/")
                .append(type)
                .append("/")
                .append(rootDir)
                .append("/")
                .append(fileName);
        return path.toString();
    }

    /**
     * 获取临时上传路径
     *
     * @param rootDir
     * @param newFileName
     * @return
     */
    public static String getTempUploadPath(String rootDir, String newFileName) {
        StringBuilder pathDir = new StringBuilder();
        String temUploadDir = UploadUtil.getTempUploadDir();
        pathDir.append(temUploadDir)
                .append("/")
                .append(rootDir)
                .append("/")
                .append(newFileName);
        return pathDir.toString();
    }

    /**
     * 获取解压目录
     *
     * @param originFileName
     * @return
     */
    public static String getUnZipDir(String originFileName) {
        StringBuilder pathDir = new StringBuilder();
        String temUploadDir = UploadUtil.getTempUploadDir();
        pathDir.append(temUploadDir)
                .append("/")
                .append(UN_ZIP_DIR)
                .append("/")
                .append(FileAnalyzerUtil.getFileNameTrimSuffix(originFileName));
        return pathDir.toString();
    }


}
